package com.easytake.emotioncat.controller;

import com.easytake.emotioncat.dto.Response;
import com.easytake.emotioncat.entity.EmFoodEntity;
import com.easytake.emotioncat.entity.EmUserInfoEntity;
import com.easytake.emotioncat.param.FoodParam;
import com.easytake.emotioncat.service.IEmDictionaryService;
import com.easytake.emotioncat.service.IEmFoodService;
import com.easytake.emotioncat.service.IEmUserInfoService;
import com.easytake.emotioncat.util.TimeUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 猫粮表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-11-11
 */
@RestController
@RequestMapping("/emFoodEntity")
public class EmFoodController {
    @Autowired
    IEmFoodService iEmFoodService;
    @Autowired
    IEmUserInfoService userInfoService;
    @Autowired
    IEmDictionaryService emDictionaryService;

    @Value("${em.food.dailyLimit:10}")
    Integer dailyLimit;

    @Operation(summary = "增加猫粮")
    @PostMapping("/add")
    public Response add(@RequestHeader String wx_uid, @Validated @RequestBody FoodParam param) {
        EmUserInfoEntity userInfoEntity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(userInfoEntity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        if (!emDictionaryService.exist(param.getCategory(), 1)) {
            throw new IllegalArgumentException("情绪类型不存在");
        }
        EmFoodEntity emFoodEntity = iEmFoodService.queryByUserIdAndCategory(wx_uid, param.getCategory());
        LocalDateTime now = LocalDateTime.now();
        if (Objects.isNull(emFoodEntity)) {
            emFoodEntity = new EmFoodEntity();
            emFoodEntity.setUserId(wx_uid);
            emFoodEntity.setCategory(param.getCategory());
            emFoodEntity.setCreateTime(now);
        }
        //如果跨天，重置今日获取猫粮数量
        if (Objects.isNull(emFoodEntity.getModifyTime()) || !TimeUtil.isSameDay(emFoodEntity.getModifyTime(), now)) {
            emFoodEntity.setTodayQuantity(0);
        }
        if (emFoodEntity.getTodayQuantity() >= dailyLimit) {
            throw new IllegalArgumentException("今日产生猫粮已经达到上限制" + dailyLimit);
        }
        emFoodEntity.setTodayQuantity(emFoodEntity.getTodayQuantity() + param.getQuantity());
        emFoodEntity.setTotalQuantity(emFoodEntity.getTotalQuantity() + param.getQuantity());
        emFoodEntity.setModifyTime(now);
        if (emFoodEntity.getTodayQuantity() > dailyLimit) {
            Integer diff = emFoodEntity.getTodayQuantity() - dailyLimit;
            emFoodEntity.setTodayQuantity(dailyLimit);
            emFoodEntity.setTotalQuantity(emFoodEntity.getTotalQuantity() - diff);
        }
        iEmFoodService.saveOrUpdate(emFoodEntity);
        return Response.success();
    }

    @Operation(summary = "查询猫粮")
    @GetMapping()
    public Response<List<EmFoodEntity>> getList(@RequestHeader String wx_uid) {
        EmUserInfoEntity userInfoEntity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(userInfoEntity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        List<EmFoodEntity> emFoodEntityList = iEmFoodService.queryByUserId(wx_uid);
        LocalDateTime now = LocalDateTime.now();
        for (EmFoodEntity en : emFoodEntityList) {
            if (Objects.isNull(en.getModifyTime()) || !TimeUtil.isSameDay(en.getModifyTime(), now)) {
                en.setTodayQuantity(0);
            }
        }
        return Response.success(emFoodEntityList);
    }


}
